package org.llc.flink.study.collection.list

object PartialFunDemo01 {
  def main(args: Array[String]): Unit = {

    val list = List(1, 2, 3, 4, "hello")

    /**
     * 方案一
     */
    val res1 = list.filter(filterFun).map(anyToInt).map(funAddOne)
    println(res1)

    /**
     * 解决方案二：使用模式匹配
     */
    val res2 = list.map(addOne)
    println(res2)
  }

  def addOne(x: Any): Any = {
    x match {
      case n: Int => n + 1
      case _ =>
    }
  }

  /**
   * @param x 输入的类型为Any类型
   * @return 输入的x为Int类型才返回true
   */
  def filterFun(x: Any): Boolean = {
    x.isInstanceOf[Int]
  }

  /**
   * @param n Any类型的参数
   * @return 返回Int
   */
  def anyToInt(n: Any): Int = {
    n.asInstanceOf[Int]
  }

  /**
   * @param n
   * @return n + 1
   */
  def funAddOne(n: Int): Int = {
    n + 1
  }

}
